package net.xdclass.online_xdclass.service.impl;

import net.xdclass.online_xdclass.model.entity.User;
import net.xdclass.online_xdclass.mapper.UserMapper;
import net.xdclass.online_xdclass.model.request.RegisterRequest;
import net.xdclass.online_xdclass.service.UserService;
import net.xdclass.online_xdclass.utils.CommonUtils;
import net.xdclass.online_xdclass.utils.JWTUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.Map;
import java.util.Random;
import java.util.UUID;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;


    @Override
    public int register(RegisterRequest request) {
        int flag;
        User exitFlag = userMapper.findByPhone(request.getPhone());
        if (exitFlag != null) {
            // 手机号已注册
            return -2;
        }
        User user = parseToUser(request);
        if (user != null) {
            flag = userMapper.save(user);
        } else {
            flag = -1;
        }
        return flag;
    }


    /**
     * 登录验证
     *
     * @param phone
     * @param password
     * @return
     */
    @Override
    public String verifyLogin(String phone, String password) {

        User user = userMapper.findByPhoneAndPwd(phone, CommonUtils.MD5(password));

        if (user == null) {
            return null;

        } else {
            String token = JWTUtils.geneJsonWebToken(user);
            return token;
        }

    }

    @Override
    public User findByUserId(String userId) {

        User user = userMapper.findByUserId(userId);
        return user;
    }

    /**
     * 解析 user 对象
     *
     * @param request
     * @return
     */
    private User parseToUser(RegisterRequest request) {
        String userName = request.getUserName();
        String phone = request.getPhone();
        String password = request.getPassword();
        if (StringUtils.isNotEmpty(phone) && StringUtils.isNotEmpty(password) && StringUtils.isNotEmpty(userName)) {
            User user = new User();
            String userId = UUID.randomUUID().toString().replace("-", "");
            user.setId(userId);
            user.setName(userName);
            user.setHeadImg(getRandomImg());
            user.setCreateTime(new Date());
            user.setPhone(phone);
            //MD5加密
            user.setPwd(CommonUtils.MD5(password));

            return user;
        } else {
            return null;
        }

    }

    /**
     * 放在CDN上的随机头像
     */
    private static final String[] headImg = {
            "https://xd-video-pc-img.oss-cn-beijing.aliyuncs.com/xdclass_pro/default/head_img/12.jpeg",
            "https://xd-video-pc-img.oss-cn-beijing.aliyuncs.com/xdclass_pro/default/head_img/11.jpeg",
            "https://xd-video-pc-img.oss-cn-beijing.aliyuncs.com/xdclass_pro/default/head_img/13.jpeg",
            "https://xd-video-pc-img.oss-cn-beijing.aliyuncs.com/xdclass_pro/default/head_img/14.jpeg",
            "https://xd-video-pc-img.oss-cn-beijing.aliyuncs.com/xdclass_pro/default/head_img/15.jpeg"
    };

    private String getRandomImg() {
        int size = headImg.length;
        Random random = new Random();
        int index = random.nextInt(size);
        return headImg[index];
    }

}
